package com.datx.wallet.controller;

import com.datx.wallet.annotation.HttpApi;
import com.datx.wallet.base.BaseResponse;
import com.datx.wallet.config.WalletProperties;
import com.datx.wallet.entity.ExchangeUserWallet;
import com.datx.wallet.mapper.ExchangeUserWalletMapper;
import com.datx.wallet.service.BaseBizService;
import com.datx.wallet.service.CommonService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * BaseController Created with IntelliJ IDEA.
 * User: liulinhui
 * Date: 2018/8/28
 * Time: 11:28
 * Description: BaseController
 */
@RestController
@RequestMapping("/rpc")
public class BaseController {
    @Resource
    public BaseBizService baseBizService;
    @Resource
    private ExchangeUserWalletMapper exchangeUserWalletMapper;
    @Resource
    private WalletProperties walletProperties;
    @Resource
    private CommonService commonService;

    /**
     * 健康检测接口
     *
     * @return BaseResponse
     */
    @RequestMapping("health")
    @HttpApi
    public BaseResponse healthCheck() {
        return baseBizService.healthCheck();
    }

    /**
     * 获取区块最新高度
     *
     * @return BaseResponse
     * @throws Exception
     */
    @RequestMapping("height")
    @HttpApi
    public BaseResponse getHeight() throws Exception {
        return BaseResponse.OK(baseBizService.getLatestBlockNum());
    }

    /**
     * 创建新账户
     *
     * @param userId 用户ID
     * @return BaseResponse
     * @throws Exception
     */
    @RequestMapping("account")
    @HttpApi
    public BaseResponse createAccount(@RequestParam("userId") String userId, @RequestParam("coinName") String coinName) throws Exception {
        String coinType = walletProperties.getCoinName();
        ExchangeUserWallet baseWallet = exchangeUserWalletMapper.selectWalletByUserAndCoinName(userId, coinType);  //主币钱包
        // 主币账户
        if (coinType.equalsIgnoreCase(coinName)) {
            // 没有账户
            if (null == baseWallet) {
                ExchangeUserWallet exchangeUserWallet = baseBizService.createAccount(userId, coinName);
                walletProperties.getUserAddressList().add(exchangeUserWallet.getAddress());
                exchangeUserWalletMapper.insertSelective(exchangeUserWallet);
                return BaseResponse.OK(exchangeUserWallet.getAddress());
            } else {
                return BaseResponse.OK(baseWallet.getAddress());
            }
        }
        //代币逻辑
        ExchangeUserWallet userWallet = exchangeUserWalletMapper.selectWalletByUserAndCoinName(userId, coinName);
        //代币存在
        if (null != userWallet) {
            return BaseResponse.OK(userWallet.getAddress());  //直接返回
        } else {
            //代币不存在,主币也不存在
            if (null == baseWallet) {
                //新建主币账户
                ExchangeUserWallet exchangeUserWallet = baseBizService.createAccount(userId, coinType);
                walletProperties.getUserAddressList().add(exchangeUserWallet.getAddress());
                exchangeUserWalletMapper.insertSelective(exchangeUserWallet);
                //新建代币
                ExchangeUserWallet assetWallet = ExchangeUserWallet.builder()
                        .coinName(coinName)
                        .userId(userId)
                        .address(exchangeUserWallet.getAddress())
                        .privateKey(exchangeUserWallet.getPrivateKey())
                        .coinType(coinType)
                        .build();
                exchangeUserWalletMapper.insertSelective(assetWallet);
                return BaseResponse.OK(assetWallet.getAddress());
            } else { //代币不存在,主币存在
                ExchangeUserWallet assetWallet = ExchangeUserWallet.builder()
                        .coinName(coinName)
                        .userId(userId)
                        .address(baseWallet.getAddress())
                        .privateKey(baseWallet.getPrivateKey())
                        .coinType(coinType)
                        .nonce(baseWallet.getNonce())
                        .build();
                exchangeUserWalletMapper.insertSelective(assetWallet);
                return BaseResponse.OK(assetWallet.getAddress());
            }
        }
    }

    /**
     * 根据user_id和货币名称获取用户地址
     *
     * @param userId   userId
     * @param coinName coinName
     * @return BaseResponse
     */
    @RequestMapping("getAddress")
    @HttpApi
    public BaseResponse getAddress(@RequestParam("userId") String userId, @RequestParam("coinName") String coinName) {
        ExchangeUserWallet exchangeUserWallet = exchangeUserWalletMapper.selectWalletByUserAndCoinName(userId, coinName);
        return BaseResponse.OK(exchangeUserWallet.getAddress());
    }


    /**
     * 获取地址余额
     *
     * @param address 货币地址
     * @param asset   货币名字(如果为主币则不传)
     * @return
     * @throws Exception
     */
    @RequestMapping("balance")
    @HttpApi
    public BaseResponse getBalanceByAddress(@RequestParam("address") String address, @RequestParam(value = "asset", required = false) String asset) throws Exception {
        return new BaseResponse(baseBizService.getBalanceByAddress(address, asset));
    }

}
